<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="ICCID" prop="iccId">
        <el-input
          v-model="queryParams.iccId"
          placeholder="请输入ICCID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="更新时间" prop="orderNumber">
        <el-date-picker
          v-model="value1"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="update"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="创建时间" prop="orderNumber">
        <el-date-picker
          v-model="value2"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="creates"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item v-show="this.swiths" label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item
        v-show="this.swiths"
        label="交易类型"
        prop="transactionType"
      >
        <el-select
          v-model="queryParams.transactionType"
          placeholder="请选择交易类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.trade_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button type="link" class="Reset" @click="zhankai">{{
          this.names
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="订单号" align="center" prop="orderNumber" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.order_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="ICCID" align="center" prop="iccid" />
      <el-table-column label="套餐" align="center" prop="packageName" />
      <el-table-column label="交易类型" align="center" prop="transactionType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.trade_type"
            :value="scope.row.transactionType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="交易金额（元）"
        align="center"
        prop="transactionAmount"
      />
      <el-table-column
        label="佣金（元）"
        align="center"
        prop="transactionAmount"
      />
      <el-table-column
        label="更新/创建时间"
        align="center"
        prop="transactionAmount"
      >
        <template slot-scope="scope">
          {{ scope.row.updateTime }}/{{ scope.row.createTime }}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item
          label="状态，0表示开通中，1表示开通成功,2表示已退款，3表示已结束"
          prop="status"
        >
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.order_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="ICCID" prop="iccid">
          <el-input v-model="form.iccid" placeholder="请输入ICCID" />
        </el-form-item>
        <el-form-item label="套餐名称" prop="packageName">
          <el-input v-model="form.packageName" placeholder="请输入套餐名称" />
        </el-form-item>
        <el-form-item
          label="交易类型,0表示充值订单，1表示线下交易"
          prop="transactionType"
        >
          <el-select
            v-model="form.transactionType"
            placeholder="请选择交易类型,0表示充值订单，1表示线下交易"
          >
            <el-option
              v-for="dict in dict.type.trade_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="交易金额" prop="transactionAmount">
          <el-input
            v-model="form.transactionAmount"
            placeholder="请输入交易金额"
          />
        </el-form-item>
        <el-form-item label="经销商ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入经销商ID" />
        </el-form-item>
        <el-form-item label="卡片ID" prop="cardId">
          <el-input v-model="form.cardId" placeholder="请输入卡片ID" />
        </el-form-item>
        <el-form-item label="套餐ID" prop="packageId">
          <el-input v-model="form.packageId" placeholder="请输入套餐ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
} from "@/api/system/order";

export default {
  name: "Order",
  dicts: ["order_status", "trade_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单管理表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        updateTimeStart: null,
        updateTimeEnd: null,
        iccId: null,
        creatTimeStart: null,
        creatTimeEnd: null,
        status: null,
        transactionType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //显示与隐藏
      swiths: false,
      //展开与收起
      names: "展开筛选",
      //更新时间
      value1: "",
      //创建时间
      value2: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    update() {
      this.queryParams.updateTimeStart = this.value1[0];
      this.queryParams.updateTimeEnd = this.value1[1];
    },
    creates() {
      this.queryParams.creatTimeStart = this.value2[0];
      this.queryParams.creatTimeEnd = this.value2[1];
    },
    //控制搜索条件展开与收起
    zhankai() {
      this.swiths = !this.swiths;
      if (this.swiths == true) {
        this.names = "收起筛选";
      }
    },
    /** 查询订单管理列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then((response) => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.queryParams = {
        updateTimeStart: "",
        updateTimeEnd: "",
        iccId: null,
        creatTimeStart: null,
        creatTimeEnd: null,
        status: null,
        transactionType: null,
        value1: "",
        value2: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOrder(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除订单管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delOrder(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/order/export",
        {
          ...this.queryParams,
        },
        `order_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
